package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pages.ProjectPage;
import com.ktjy.pojo.SalChance;
import com.ktjy.pojo.SysUser;
import com.ktjy.pojo.vo.SysUserVo;
import com.ktjy.service.SalChanceService;
import com.ktjy.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 销售机会管理
 */
@Controller
public class ChancePlanController {
    @Autowired
    SalChanceService salChanceService;
    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/chance/list")
    public Object chanceList(String custName, String chcDesc, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "3") Integer pageSize, Model model) {

        //获取当前登录人的id
        //getSubject 主体
        //getPrincipal 认证的对象
        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        IPage<SalChance> salChanceIPage = new Page<>(pageIndex, pageSize);
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(custName != null, "chc_cust_name", custName).like(chcDesc != null, "chc_desc", chcDesc)
                .eq(sysUserVo.getRole().getRoleId() == 2 && sysUserVo.getUsrId() != 1, "chc_due_id", sysUserVo.getUsrId());
        Page<SalChance> page = (Page<SalChance>) salChanceService.page(salChanceIPage, queryWrapper);
        ProjectPage<SalChance> projectPage = new ProjectPage<>();
        BeanUtils.copyProperties(page, projectPage);
        model.addAttribute("chancePage", projectPage);
        return "/chance/list";
    }

    @RequestMapping("/chance/add")
    public Object chanceAdd(Model model) {
        model.addAttribute("users", sysUserService.list());
        return "/chance/add";
    }
    @RequestMapping("/chance/del/{chcId}")
    @ResponseBody
    public Object chanceDel(@PathVariable Long chcId,Model model) {
        QueryWrapper<SalChance> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(SalChance::getChcId, chcId).isNotNull(SalChance::getChcStatus);
        Map<String,Object> map = new HashMap<>();
        boolean flg = salChanceService.remove(queryWrapper);
        String delResult = null;
        if (flg){
            delResult = "true";
        }else {
            delResult = "false";
        }
        map.put("delResult",delResult );
        return map;
    }

    @RequestMapping("/chance/edit")
    public Object chanceEdit(Long chcId,Model model) {
        model.addAttribute("users", sysUserService.list());
        model.addAttribute("chance", salChanceService.getById(chcId));
        return "/chance/edit";
    }

    @RequestMapping("/chance/save")
    public Object chanceSave(SalChance salChance) {
        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
        if (salChance != null) {
            //创建人姓名
            salChance.setChcCreateBy(sysUserVo.getUsrName());
            //创建人id
            salChance.setChcCreateId(sysUserVo.getUsrId());
            //被指派人姓名
            SysUserVo sysUserVo1 = sysUserService.getById(salChance.getChcDueId());
            salChance.setChcDueTo(sysUserVo1.getUsrName());
            boolean flg = salChanceService.saveOrUpdate(salChance);
            if (flg) {
                return "redirect:/chance/list";
            } else {
                return "/chance/add";
            }
        } else {
            return "/chance/add";
        }
    }
}
